package com.tongchengsoft.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.tongchengsoft.bean.BeanDepartmentTable;
import com.tongchengsoft.server.IDepartmentTableService;
import com.tongchengsoft.tools.StringTools;

/**
 * 部门控制器
 * @author Wang
 *
 */
@Controller
@RequestMapping("department")
public class DepartmentTableController {

	@Autowired
	private IDepartmentTableService departmentTableService;
	
	@RequestMapping(value = "/addJump")
	public String AddJump(HttpServletRequest request, HttpServletResponse response){
		
		return "departmentTable/add";
	}
	
	
	@RequestMapping(value = "/add")
	public ModelAndView Add(HttpServletRequest request, HttpServletResponse response){
		
		String bmMc = request.getParameter("bmMc");
		BeanDepartmentTable departmentTable = new BeanDepartmentTable(bmMc);
		ModelMap mm = new ModelMap();
		
		try {
			
			if(departmentTableService.add(departmentTable))
				
				mm.put("msg", "添加成功！");
			else
				
				mm.put("msg", "添加失败！");
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return new ModelAndView("redirect:findAll", mm);
	}
	
	@RequestMapping(value = "/delete")
	public ModelAndView Delete(HttpServletRequest request, HttpServletResponse response){
		
		String lsId = request.getParameter("lsId");
		ModelMap mm = new ModelMap();
		
		try {
			
			if(StringTools.isEmpty(lsId) || !StringTools.isNumber(lsId)){
				
				mm.put("msg", "删除失败！");
				return new ModelAndView("findAll", mm);
			}
			BeanDepartmentTable departmentTable = new BeanDepartmentTable(Integer.valueOf(lsId));
			if(departmentTableService.delete(departmentTable))
				
				mm.put("msg", "删除成功！");
			else
				
				mm.put("msg", "删除失败！");
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return new ModelAndView("redirect:findAll", mm);
	}
	
	@RequestMapping(value = "/updateJump")
	public ModelAndView UpdateJump(HttpServletRequest request, HttpServletResponse response){
		
		String lsId = request.getParameter("lsId");
		ModelMap mm = new ModelMap();
		
		try {
			
			if(StringTools.isEmpty(lsId) || !StringTools.isNumber(lsId)){
				
				mm.put("msg", "获取部门信息失败！");
				return new ModelAndView("redirect:findAll", mm);
			}
			BeanDepartmentTable bdt =  departmentTableService.findByLsId(Integer.valueOf(lsId));
			request.setAttribute("bdt", bdt);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return new ModelAndView("departmentTable/update");
	}
	
	@RequestMapping(value = "/update")
	public ModelAndView Update(HttpServletRequest request, HttpServletResponse response){
		
		String lsId = request.getParameter("lsId");
		String bmMc = request.getParameter("bmMc");
		ModelMap mm = new ModelMap();
		
		try {
			
			if(StringTools.isEmpty(lsId) || !StringTools.isNumber(lsId)){
				
				mm.put("msg", "修改失败！");
				return new ModelAndView("redirect:findAll", mm);
			}
			BeanDepartmentTable departmentTable = new BeanDepartmentTable(Integer.valueOf(lsId), bmMc);
			if(departmentTableService.update(departmentTable))
				
				mm.put("msg", "修改成功！");
			else
				
				mm.put("msg", "修改失败！");
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return new ModelAndView("redirect:findAll", mm);
	}
	
	@RequestMapping(value = "/findAll")
	public ModelAndView FindAll(HttpServletRequest request, HttpServletResponse response){
		
		ModelMap mm = new ModelMap();
		
		try {
			mm.put("msg", request.getAttribute("msg"));
			List<BeanDepartmentTable> dtList = departmentTableService.find(null);
			request.setAttribute("dtList", dtList);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return new ModelAndView("departmentTable/list", mm);
	}
	
}
